/**
 * 带滴管瓶子（BaCl2，NaOH,NaCO3）
 */
DropBottle = cc.Node.extend({
	labelTag:0,
	ctor:function(parent,tag){
		this._super();
		parent.addChild(this,10,tag);
		this.init(tag);
		this.initLabel(tag);
	},
	init:function(tag){
		var drop = new cc.Sprite("#drop_bottle.png");
		this.addChild(drop, 5);
		if(TAG_NAOH == tag){
			var drop2 = new Button(this, 3, TAG_DROP, "#drop_bottle2.png",this.callback);
			drop2.setPosition(0, 34);
		}else{
			var drop2 = new Button(this, 3, TAG_DROP, "#drop_bottle2.png",this.callback);
			drop2.setPosition(0, 34);
		}	
	},
	initLabel:function(tag){
		this.labelTag = tag;
		var name = "";
		if(TAG_BACL == tag){
			name = "BaCl2";
		} else if(TAG_NA2CO3 == tag){
			name = "Na2CO3";
		} else if(TAG_NAOH == tag){
			name = "NAOH";
		} else if(TAG_HCL == tag){
			name = "HCL";
		} else if(TAG_ALCOHOL == tag){
			name = "酒精";
		}
		var label = new cc.LabelTTF(name,gg.fontName,12);
		label.setColor(cc.color(0,0,0));
		label.setPosition(0, -10);
		this.addChild(label, 11);
	},
	genPoint:function(p,count){
		count --;
		if(count <= 0){
			return;
		}
		var wp = new cc.Sprite("#wpoint.png")
		wp.setPosition(10, 0);
		p.addChild(wp);
		var move = cc.moveBy(0.5, cc.p(0, -45));
		var func = cc.callFunc(function(){
			wp.removeFromParent(true);
			this.genPoint(p,count);
		}, this);
		var seq = cc.sequence(move, func);
		wp.runAction(seq);
	},
	bezier:function(orig,target,time){
		var my = orig.y > target.y ? orig.y - target.y : target.y - orig.y;
		var pos1 = cc.p(orig.x, target.y + my / 2);
		var pos2 = cc.p(target.x, target.y + my / 2);
		var pos3 = target;
		return cc.bezierTo(time, [pos1, pos2, pos3]);
	},
	showTip:function(){
		var point = this.getChildByTag(TAG_DROP);
		var bound = point.getBoundingBox();
		var tx = this.getPositionX();
		var ty = this.getPositionY();
		switch (this.getTag()) {
		case TAG_BACL:
			var formula = new Formula("#formula/1.png");
			formula.setPosition(220 + tx, 50 + ty);
			break;
		case TAG_NA2CO3:
			var formula = new Formula("#formula/2.png");
			formula.setPosition(220 + tx, 50 + ty);
			break;
		case TAG_NAOH:
			var formula = new Formula("#formula/2.png");
			formula.setPosition(220 + tx, 50 + ty);
			break;
		case TAG_HCL:
			var formula = new Formula("#formula/3.png");
			formula.setPosition(20 + tx, 50 + ty);
			break;
		case TAG_ALCOHOL:
			break;
		}
	},
	callback:function(p){
		switch(p.getTag()){
		case TAG_DROP:
			this.showTip();
			// 往滤液中加入bacl2
			var action = gg.flow.flow.action;
			var ber = null, pos = p.getPosition();
			if(this.labelTag == TAG_HCL){
				ber = this.bezier(pos, cc.p(-130,130), 0.6);
			} else if(this.labelTag == TAG_ALCOHOL){
				ber = this.bezier(pos, cc.p(-112,190), 0.6);
			} else {
				ber = this.bezier(pos, cc.p(165,180), 0.6);
			}
			var func = cc.callFunc(function(){
				// 滴下水滴
				this.genPoint(p, 4);
				if(action == ACTION_DO1){
					// 烧杯
					var beaker = this.getParent().getChildByTag(TAG_BEAKER2);
					beaker.showDep();
				} else if(action == ACTION_DO3){
					var beaker = this.getParent().getChildByTag(TAG_BEAKER3);
					beaker.showDep();
				}
			},this);
			var delay = cc.delayTime(2);
			var move2 = cc.moveTo(0.6, cc.p(0, 45));
			var func2 = cc.callFunc(function(){
				this.setCascadeOpacityEnabled(true);
				this.runAction(cc.fadeTo(0.5,0));
				gg.flow.next();
			}, this);
			var seq = cc.sequence(ber,func,delay,move2,func2);
			p.runAction(seq);
			break;
		default:
			break;
		}
	}
});